package com.scau.ums.service.imp.repair;

import com.scau.ums.mapper.RepairevaluationMapper;
import com.scau.ums.model.Repairevaluation;
import com.scau.ums.model.RepairevaluationExample;
import com.scau.ums.model.Repairreport;
import com.scau.ums.service.EvaluateRepair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * author: 莫伟杰
 * date： 2019/7/29 16:16
 */
@Service
public class EvaluateRepairImp implements EvaluateRepair {

    private UserRepairReportImp repairReportImp;
    private RepairevaluationMapper mapper;

    @Autowired
    public EvaluateRepairImp(UserRepairReportImp repairReportImp,RepairevaluationMapper mapper) {
        this.repairReportImp=repairReportImp;
        this.mapper = mapper;
    }

    @Override
    public boolean evaluateRepair(int reportId, String evaluation, String remark) {
        Repairreport repairreport=repairReportImp.getRepairreportById(reportId);
        if(repairreport==null) return false;
        Repairevaluation repairevaluation = new Repairevaluation();
        repairevaluation.setId(0);
        repairevaluation.setEvaluation(evaluation);
        repairevaluation.setRemark(remark);
        repairevaluation.setRepairreportid(reportId);
        //将状态设置为7（结束）
        repairreport.setState(7);
        repairReportImp.updateRepairreport(repairreport);
        //判断插入成功与否
        return mapper.insert(repairevaluation)==1;
    }

    @Override
    public Repairevaluation getRepairEvaluationByReportId(Integer repairReportId) {
        RepairevaluationExample example = new RepairevaluationExample();
        example.createCriteria().andRepairreportidEqualTo(repairReportId);
        List<Repairevaluation> list = mapper.selectByExample(example);
        if(list.isEmpty()) return null;
        //获取第一个结果
        else return list.get(0);
    }
}
